package com.lanou.dllo.vc.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.lanou.dllo.vc.sdkhelpter.HXSDKHelper;

/**
 * Created by dllo on 15/11/20.
 */
public class ChatBroadcast extends BroadcastReceiver {

//    protected NotificationManager notificationManager = null;
//
//    protected static int foregroundNotifyID = 0555;




    @Override
    public void onReceive(Context context, Intent intent) {
        // 注销广播
//        abortBroadcast();

//        // 消息id（每条消息都会生成唯一的一个id，目前是SDK生成）
//        String msgId = intent.getStringExtra("msgid");
//        //发送方
//        String username_from = intent.getStringExtra("from");
//        // 收到这个广播的时候，message已经在db和内存里了，可以通过id获取mesage对象
//        EMMessage message = EMChatManager.getInstance().getMessage(msgId);
//        EMConversation conversation = EMChatManager.getInstance().getConversation(username);
//        // 如果是群聊消息，获取到group id
//        if (message.getChatType() == EMMessage.ChatType.GroupChat) {
//            username = message.getTo();
//        }
//        if (!username.equals(username)) {
//            // 消息不是发给当前会话，return
//            return;
//        }
////        String alert = intent.getStringExtra("alyer");
////        sendNotification(alert, true);
//    }


//    private void sendNotification(String message, boolean isForeground) {
//        Context context = HXSDKHelper.getInstance().getAppContext();
//
//        if (notificationManager == null) {
//            notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//
//        }
//
//
//        try {
//            String notifyText = message;
//            PackageManager packageManager = context.getPackageManager();
//            String appname = (String) packageManager.getApplicationLabel(context.getApplicationInfo());
//            String contentTitle = appname;
//            String packageName = context.getApplicationInfo().packageName;
//            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//            NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(context.getApplicationInfo().icon)
//                    .setSound(defaultSoundUri).setWhen(System.currentTimeMillis()).setAutoCancel(true);
//
//            Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
//            PendingIntent pendingIntent = PendingIntent.getActivity(context, foregroundNotifyID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//
//            builder.setContentTitle(contentTitle);
//            builder.setTicker(notifyText);
//            builder.setContentText(notifyText);
//            builder.setContentIntent(pendingIntent);
//            Notification notification = builder.build();
//
//            notificationManager.notify(foregroundNotifyID, notification);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
        //消息id
        String msgId = intent.getStringExtra("msgid");
        //发消息的人的username(userid)
        String msgFrom = intent.getStringExtra("from");
        //消息类型，文本，图片，语音消息等,这里返回的值为msg.type.ordinal()。
        //所以消息type实际为是enum类型
        int msgType = intent.getIntExtra("type", 0);
        Log.d("main", "new message id:" + msgId + " from:" + msgFrom + " type:" + msgType);
        //更方便的方法是通过msgId直接获取整个message
        EMMessage message = EMChatManager.getInstance().getMessage(msgId);

    }
}

